<!DOCTYPE HTML PUBLIC "-//ORA//DTD CD HTML 3.2//EN">
<HTML>
<HEAD>
<TITLE>[Chapter 2] 2.2 StringBuffer</TITLE>
<META NAME="author" CONTENT="Mark Grand and Jonathan Knudsen">
<META NAME="date" CONTENT="Fri Aug  8 16:07:54 1997">
<META NAME="form" CONTENT="html">
<META NAME="metadata" CONTENT="dublincore.0.1">
<META NAME="objecttype" CONTENT="book part">
<META NAME="otheragent" CONTENT="gmat dbtohtml">
<META NAME="publisher" CONTENT="O'Reilly &amp; Associates, Inc.">
<META NAME="source" CONTENT="SGML">
<META NAME="subject" CONTENT="Java">
<META NAME="title" CONTENT="Java Fundamental Classes Reference">
<META HTTP-EQUIV="Content-Script-Type" CONTENT="text/javascript">
</HEAD>
<body vlink="#551a8b" alink="#ff0000" text="#000000" bgcolor="#FFFFFF" link="#0000ee">

<DIV CLASS=htmlnav>
<H1><a href='index.htm'><IMG SRC="gifs/smbanner.gif"
     ALT="Java Fundamental Classes Reference" border=0></a></H1>
<table width=515 border=0 cellpadding=0 cellspacing=0>
<tr>
<td width=172 align=left valign=top><A HREF="ch02_01.htm"><IMG SRC="gifs/txtpreva.gif" ALT="Previous" border=0></A></td>
<td width=171 align=center valign=top><B><FONT FACE="ARIEL,HELVETICA,HELV,SANSERIF" SIZE="-1">Chapter 2<br>Strings and Related Classes</FONT></B></TD>
<td width=172 align=right valign=top><A HREF="ch02_03.htm"><IMG SRC="gifs/txtnexta.gif" ALT="Next" border=0></A></td>
</tr>
</table>

&nbsp;
<hr align=left width=515>
</DIV>
<DIV CLASS=sect1>
<h2 CLASS=sect1><A CLASS="TITLE" NAME="JFC-CH-2-SECT-2">2.2 StringBuffer</A></h2>

<P CLASS=para>
<tt CLASS=literal>StringBuffer</tt> objects are similar 
to <tt CLASS=literal>String</tt> objects in that they 
both represent sequences of characters. The main difference is that a <tt CLASS=literal>StringBuffer</tt> 
is mutable, while a <tt CLASS=literal>String</tt> 
is not. The <tt CLASS=literal>StringBuffer</tt> class 
defines a number of <tt CLASS=literal>append()</tt> 
methods for adding characters to the end of the sequence, as well as a 
number of <tt CLASS=literal>insert()</tt> methods 
for inserting characters anywhere in the sequence. 

<P CLASS=para>
Many computations that produce a <tt CLASS=literal>String</tt> 
object use a <tt CLASS=literal>StringBuffer</tt> internally 
to build the string. For example, to write a method that takes a <tt CLASS=literal>String</tt> 
object and returns a new <tt CLASS=literal>String</tt> 
that contains the sequence of characters in reverse, you use a <tt CLASS=literal>StringBuffer</tt> 
as follows: 

<DIV CLASS=screen>
<P>
<PRE>
public static String reverse(String s) {
    StringBuffer buf = new StringBuffer(s.length());
    for (int i = s.length()-1; i &gt;= 0; i--) {
        buf.append(s.charAt(i));
    }
    return buf.toString();
}
</PRE>
</DIV>

<P CLASS=para>
After creating a new <tt CLASS=literal>StringBuffer</tt> 
object, the method loops over the given string from the last character 
to the first character and appends each character to the end of the <tt CLASS=literal>StringBuffer</tt> 
object. When the loop finishes, the <tt CLASS=literal>StringBuffer</tt> 
object contains a sequence of characters that is the reverse of the sequence 
of characters in the given <tt CLASS=literal>String</tt> 
object. The method finishes by calling the <tt CLASS=literal>toString()</tt> 
method of the <tt CLASS=literal>StringBuffer</tt>; 
this method returns a <tt CLASS=literal>String</tt> 
object that contains the same sequence of characters as the <tt CLASS=literal>StringBuffer</tt> 
object. 

</DIV>


<DIV CLASS=htmlnav>

<P>
<HR align=left width=515>
<table width=515 border=0 cellpadding=0 cellspacing=0>
<tr>
<td width=172 align=left valign=top><A HREF="ch02_01.htm"><IMG SRC="gifs/txtpreva.gif" ALT="Previous" border=0></A></td>
<td width=171 align=center valign=top><a href="index.htm"><img src='gifs/txthome.gif' border=0 alt='Home'></a></td>
<td width=172 align=right valign=top><A HREF="ch02_03.htm"><IMG SRC="gifs/txtnexta.gif" ALT="Next" border=0></A></td>
</tr>
<tr>
<td width=172 align=left valign=top>String</td>
<td width=171 align=center valign=top><a href="index/idx_0.htm"><img src='gifs/index.gif' alt='Book Index' border=0></a></td>
<td width=172 align=right valign=top>String Concatenation</td>
</tr>
</table>
<hr align=left width=515>

<IMG SRC="gifs/smnavbar.gif" USEMAP="#map" BORDER=0> 
<MAP NAME="map"> 
<AREA SHAPE=RECT COORDS="0,0,108,15" HREF="../javanut/index.htm"
alt="Java in a Nutshell"> 
<AREA SHAPE=RECT COORDS="109,0,200,15" HREF="../langref/index.htm" 
alt="Java Language Reference"> 
<AREA SHAPE=RECT COORDS="203,0,290,15" HREF="../awt/index.htm" 
alt="Java AWT"> 
<AREA SHAPE=RECT COORDS="291,0,419,15" HREF="../fclass/index.htm" 
alt="Java Fundamental Classes"> 
<AREA SHAPE=RECT COORDS="421,0,514,15" HREF="../exp/index.htm" 
alt="Exploring Java"> 
</MAP>
</DIV>

</BODY>
</HTML>
